package demo2;

import java.util.Stack;

public class MinStack {
    public Stack<Integer> stack;
    public Stack<Integer> minstack;
    public MinStack(){
        stack = new Stack<>();
        minstack = new Stack<>();
    }
    public void pop(){
        int topval = stack.pop();
        if(topval == minstack.peek()){
            minstack.pop();
        }
    }
    public void push(int val){
        stack.push(val);
        if(minstack.isEmpty()){
            minstack.push(val);
        }else{
            int topval = minstack.peek();
            if(val <= topval){
                minstack.push(val);
            }
        }
    }
    public int top(){
        return stack.peek();
    }
    public int getMin(){
        return minstack.peek();
    }
}
